/**
 * Created by Forcs on 16/3/29.
 */
import $ from 'jquery'
import Highcharts from 'highcharts'
import WCommons from '../../../../static/js/wanba.commons'

export default {
  props: {
    chartAdapter: {
      type: Object,
      require: true
    },
    chartTitle: String,
    chartWidth: {
      type: Number
    },
    chartHeight: {
      type: Number,
      default: 200
    },
    valueSuffix: String
  },
  created: function () {
    var self = this
    WCommons.extends(self.chartAdapter || {}, {
      component: self,
      notifyDatasetChanged: function () {
        var ada = this
        if (ada.component && ada.component.$options.chart) {
          if (ada.component.$options.resolveDatasetChanged) {
            ada.component.$options.resolveDatasetChanged.call(ada.component)
          }
        }
      }
    })
  },
  attached: function () {
    var self = this
    if (self.$options.chart) {
      return
    }
    var chartOptions = self.$options.preConfigChartOptions.call(self)
    if (!self.chartWidth && !chartOptions.chart.width) {
      chartOptions.chart.width = $(self.el).parent().width()
    }
    if (!self.chartHeight && !chartOptions.chart.height) {
      chartOptions.chart.height = $(self.el).parent().height()
    }
    self.$options.chart = new Highcharts.Chart(self.$el, chartOptions)
  },
  detached: function () {
    var self = this
    if (self.$options.chart) {
      self.$options.chart.destroy()
      self.$options.chart = undefined
    }
  }
}
